
*All exposure measure by separating for industries

use "$list/czones_list.dta", clear
save "$clean_data_automation/czones_exposure_robots_industries.dta", replace

foreach industry in car elc fod frn mot mbs mmc mpr mnr ppr ptr txt vho cst mng rsc ser agr uti { 

**measure only for industry of interest for us**
use "$clean_data_lmarket/czone_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate

replace industry_ifr19="car" if industry_ifr19=="automotive"
replace industry_ifr19="elc" if industry_ifr19=="electronics"
replace industry_ifr19="fod" if industry_ifr19=="food"
replace industry_ifr19="frn" if industry_ifr19=="furniture"
replace industry_ifr19="mot" if industry_ifr19=="manufacturing_other"
replace industry_ifr19="mbs" if industry_ifr19=="metal_basic"
replace industry_ifr19="mmc" if industry_ifr19=="metal_machinery"
replace industry_ifr19="mpr" if industry_ifr19=="metal_products"
replace industry_ifr19="mnr" if industry_ifr19=="mineral"
replace industry_ifr19="ppr" if industry_ifr19=="paper"
replace industry_ifr19="ptr" if industry_ifr19=="petrochemicals"
replace industry_ifr19="txt" if industry_ifr19=="textiles"
replace industry_ifr19="vho" if industry_ifr19=="vehicles_other"
replace industry_ifr19="cst" if industry_ifr19=="construction"
replace industry_ifr19="mng" if industry_ifr19=="mining"
replace industry_ifr19="rsc" if industry_ifr19=="research"
replace industry_ifr19="ser" if industry_ifr19=="services"
replace industry_ifr19="agr" if industry_ifr19=="agriculture"
replace industry_ifr19="uti" if industry_ifr19=="utilities"

keep industry_ifr19 czone emppriv apr_us_adj93_00_ apr_us_adj00_07_ apr_us_adj07_14_
foreach var of varlist apr_*{
replace `var'=0 if industry_ifr!="`industry'"
}
collapse (mean) apr_us* [w=emppriv], by(czone) fast
rename apr_* expo_`industry'_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots_industries.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots_industries.dta", replace

**measure only for industry of interest for instrument**
use "$clean_data_lmarket/czone_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate

replace industry_ifr19="car" if industry_ifr19=="automotive"
replace industry_ifr19="elc" if industry_ifr19=="electronics"
replace industry_ifr19="fod" if industry_ifr19=="food"
replace industry_ifr19="frn" if industry_ifr19=="furniture"
replace industry_ifr19="mot" if industry_ifr19=="manufacturing_other"
replace industry_ifr19="mbs" if industry_ifr19=="metal_basic"
replace industry_ifr19="mmc" if industry_ifr19=="metal_machinery"
replace industry_ifr19="mpr" if industry_ifr19=="metal_products"
replace industry_ifr19="mnr" if industry_ifr19=="mineral"
replace industry_ifr19="ppr" if industry_ifr19=="paper"
replace industry_ifr19="ptr" if industry_ifr19=="petrochemicals"
replace industry_ifr19="txt" if industry_ifr19=="textiles"
replace industry_ifr19="vho" if industry_ifr19=="vehicles_other"
replace industry_ifr19="cst" if industry_ifr19=="construction"
replace industry_ifr19="mng" if industry_ifr19=="mining"
replace industry_ifr19="rsc" if industry_ifr19=="research"
replace industry_ifr19="ser" if industry_ifr19=="services"
replace industry_ifr19="agr" if industry_ifr19=="agriculture"
replace industry_ifr19="uti" if industry_ifr19=="utilities"

keep industry_ifr19 czone emppriv apr_euro7_qo93_00_ apr_euro7_qo00_07_ apr_euro7_qo07_14_
foreach var of varlist apr_*{
replace `var'=0 if industry_ifr!="`industry'"
}
collapse (mean) apr_* [w=emppriv], by(czone) fast
rename apr_* expo_`industry'_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots_industries.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots_industries.dta", replace

**measure only for other industries for us**
use "$clean_data_lmarket/czone_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate

replace industry_ifr19="car" if industry_ifr19=="automotive"
replace industry_ifr19="elc" if industry_ifr19=="electronics"
replace industry_ifr19="fod" if industry_ifr19=="food"
replace industry_ifr19="frn" if industry_ifr19=="furniture"
replace industry_ifr19="mot" if industry_ifr19=="manufacturing_other"
replace industry_ifr19="mbs" if industry_ifr19=="metal_basic"
replace industry_ifr19="mmc" if industry_ifr19=="metal_machinery"
replace industry_ifr19="mpr" if industry_ifr19=="metal_products"
replace industry_ifr19="mnr" if industry_ifr19=="mineral"
replace industry_ifr19="ppr" if industry_ifr19=="paper"
replace industry_ifr19="ptr" if industry_ifr19=="petrochemicals"
replace industry_ifr19="txt" if industry_ifr19=="textiles"
replace industry_ifr19="vho" if industry_ifr19=="vehicles_other"
replace industry_ifr19="cst" if industry_ifr19=="construction"
replace industry_ifr19="mng" if industry_ifr19=="mining"
replace industry_ifr19="rsc" if industry_ifr19=="research"
replace industry_ifr19="ser" if industry_ifr19=="services"
replace industry_ifr19="agr" if industry_ifr19=="agriculture"
replace industry_ifr19="uti" if industry_ifr19=="utilities"

keep industry_ifr19 czone emppriv apr_us_adj93_00_ apr_us_adj00_07_ apr_us_adj07_14_
foreach var of varlist apr_*{
replace `var'=0 if industry_ifr=="`industry'"
}
collapse (mean) apr_* [w=emppriv], by(czone) fast
rename apr_* expon_`industry'_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots_industries.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots_industries.dta", replace


**measure only for other industries for instrument**
use "$clean_data_lmarket/czone_1970_emp_by_ifr19.dta", clear
merge m:1 industry_ifr19 using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate

replace industry_ifr19="car" if industry_ifr19=="automotive"
replace industry_ifr19="elc" if industry_ifr19=="electronics"
replace industry_ifr19="fod" if industry_ifr19=="food"
replace industry_ifr19="frn" if industry_ifr19=="furniture"
replace industry_ifr19="mot" if industry_ifr19=="manufacturing_other"
replace industry_ifr19="mbs" if industry_ifr19=="metal_basic"
replace industry_ifr19="mmc" if industry_ifr19=="metal_machinery"
replace industry_ifr19="mpr" if industry_ifr19=="metal_products"
replace industry_ifr19="mnr" if industry_ifr19=="mineral"
replace industry_ifr19="ppr" if industry_ifr19=="paper"
replace industry_ifr19="ptr" if industry_ifr19=="petrochemicals"
replace industry_ifr19="txt" if industry_ifr19=="textiles"
replace industry_ifr19="vho" if industry_ifr19=="vehicles_other"
replace industry_ifr19="cst" if industry_ifr19=="construction"
replace industry_ifr19="mng" if industry_ifr19=="mining"
replace industry_ifr19="rsc" if industry_ifr19=="research"
replace industry_ifr19="ser" if industry_ifr19=="services"
replace industry_ifr19="agr" if industry_ifr19=="agriculture"
replace industry_ifr19="uti" if industry_ifr19=="utilities"

keep industry_ifr19 czone emppriv apr_euro7_qo93_00_ apr_euro7_qo00_07_ apr_euro7_qo07_14_
foreach var of varlist apr_*{
replace `var'=0 if industry_ifr=="`industry'"
}
collapse (mean) apr_* [w=emppriv], by(czone) fast
rename apr_* expon_`industry'_*
merge 1:1 czone using "$clean_data_automation/czones_exposure_robots_industries.dta", assert(3) nogenerate 
save "$clean_data_automation/czones_exposure_robots_industries.dta", replace
}


use "$clean_data_automation/czones_exposure_robots_industries.dta", clear

*Keep relevant dates*
rename *93_00_ *_y1990
rename *00_07_ *_y2000
rename *07_14_ *_y2007

keep czone-name_czone *_y1990 *_y20*

*Create stacked differences*
keep czone *_y19* *_y20*
order *_y19* *_y20*

*obtain list of variables to reshape*
quietly: describe *y1990, varlist
local varlist = r(varlist)
local finalvar: subinstr local varlist "_y1990" "_y", all
reshape long `finalvar' , i(czone) j(year)
rename *_y *

replace year = 2008 if year == 2007

*Save*
save "$final_data_automation/czones_AutomationExposureIndustry.dta", replace
